<?php

require_once 'helper/DbTable.php';
class Groupship extends DbTable
{
    public static $_TABLE_NAME_ = 'yxb_groupship';
    public static $required = ['gp_id','user_id'];
    public static $members_mapping = ['gp_id','gs_id','user_id'];
    public static $relationship = [];
    public static $extends_mapping = [];

    private $gp_id;
    private $gs_id;
    private $user_id;


    public function copy()
    {
        // TODO: Implement copy() method.
        $temp = new self();
        foreach (self::$members_mapping as $item) {
            DbTable::callSetMethod($temp, $item, DbTable::callGetMethod($this, $item));
        }
        return $temp;
    }

    public function changeTo($obj)
    {
        // TODO: Implement changeTo() method.
        foreach (self::$members_mapping as $item) {
            DbTable::callSetMethod($this, $item, DbTable::callGetMethod($obj, $item));
        }
    }

    public static function getTableName()
    {
        // TODO: Implement getTableName() method.
        return self::$_TABLE_NAME_;
    }

    public static function getRequired()
    {
        // TODO: Implement getRequired() method.
        return self::$required;
    }

    public function loadData($data)
    {
        // TODO: Implement loadData() method.
        $temp = array_merge(self::$members_mapping,self::$extends_mapping);
        foreach ($temp as $member) {

            if (array_key_exists($member, $data)) {
                DbTable::callSetMethod($this, $member, $data[$member]);
            } else {
                return false;
            }
        }
        return true;
    }

    public function add()
    {
        // TODO: Implement add() method.
        $format = "insert into %s(%s) values(%s);";
        $attr = [];
        $values = [];
        foreach (self::$members_mapping as $item) {
            $value = DbTable::callGetMethod($this, $item);
            if ($item == "gs_id" || $value == null)
                continue;
            array_push($attr, $item);
            is_numeric($value) ? array_push($values, "$value") : array_push($values, "\"$value\"");
        }
        $sql = sprintf($format, self::$_TABLE_NAME_, join(",", $attr), join(",", $values));
        return $sql;
    }

    public function delete()
    {
        // TODO: Implement delete() method.
        $format = "delete from %s where user_id=%s;";

        $sql = sprintf($format, self::$_TABLE_NAME_, $this->getUserId());
        return $sql;
    }

    public function edit($data)
    {
        // TODO: Implement edit() method.
        $format = "update %s set %s where user_id=%s;";
        $changeSql = [];
        foreach (self::$members_mapping as $item) {
            if (isset($data[$item]) && $item != "user_id"&&$item!='gs_id') {
                DbTable::callsetMethod($this, $item, $data[$item]);
                is_numeric($data[$item]) ? array_push($changeSql, $item . " = " . $data[$item]) : array_push($changeSql, $item . "= \"" . $data[$item] . "\"");;
            }
        }
        $sql = null;
        if (count($changeSql) >= 1)
            $sql = sprintf($format, self::$_TABLE_NAME_, join(",", $changeSql), $this->getUserId());
        return $sql;
    }

    public static function query($condition)
    {
        return null;
    }

    /**
     * @return mixed
     */
    public function getGpId()
    {
        return $this->gp_id;
    }

    /**
     * @param mixed $gp_id
     */
    public function setGpId($gp_id)
    {
        $this->gp_id = $gp_id;
    }

    /**
     * @return mixed
     */
    public function getGsId()
    {
        return $this->gs_id;
    }

    /**
     * @param mixed $gs_id
     */
    public function setGsId($gs_id)
    {
        $this->gs_id = $gs_id;
    }

    /**
     * @return mixed
     */
    public function getUserId()
    {
        return $this->user_id;
    }

    /**
     * @param mixed $user_id
     */
    public function setUserId($user_id)
    {
        $this->user_id = $user_id;
    }

}